1 module hip.windowing.platforms.x11lib.x11;
2 
3 version(X11):
4 
5 import hip.windowing.platforms.x11lib.glx;
6 import core.stdc.config;
7 
8 extern(C):
9 
10 
11 /**
12 *   I'll define here a minimal set of x11 functions.
13 */
14 alias XID = c_ulong;
15 alias Atom = c_ulong;
16 alias VisualID = c_ulong;
17 alias Time = c_ulong;
18 
19 struct _XGC;
20 alias GC = _XGC*;
21 struct Depth;
22 struct ScreenFormat;
23 struct _XrmHashBucketRec;
24 struct _XPrivate;
25 struct XExtData;
26 alias XErrorHandler = extern(C) nothrow @nogc int function(Display*, XErrorEvent*);
27 struct _XDisplay
28 {
29 	import core.stdc.config:c_ulong;
30     XExtData* ext_data;                                 /* hook for extension to hang data                              */
31     _XPrivate* private1;
32     int fd;                                             /* Network socket.                                              */
33     int private2;
34     int proto_major_version;                            /* major version of server's X protocol */
35     int proto_minor_version;                            /* minor version of servers X protocol */
36     char* vendor;                                       /* vendor of the server hardware */
37     XID private3;
38     XID private4;
39     XID private5;
40     int private6;
41     extern (C) nothrow XID function(_XDisplay*) resource_alloc;             /* allocator function */
42     int char_order;                                     /* screen char order, LSBFirst, MSBFirst */
43     int bitmap_unit;                                    /* padding and data requirements */
44     int bitmap_pad;                                     /* padding requirements on bitmaps */
45     int bitmap_bit_order;                               /* LeastSignificant or MostSignificant */
46     int nformats;                                       /* number of pixmap formats in list */
47     ScreenFormat* pixmap_format;                        /* pixmap format list */
48     int private8;
49     int release;                                        /* release of the server */
50     _XPrivate* private9, private10;
51     int qlen;                                           /* Length of input event queue */
52     c_ulong  last_request_read;                         /* seq number of last event read */
53     c_ulong  request;                                   /* sequence number of last request. */
54     XPointer private11;
55     XPointer private12;
56     XPointer private13;
57     XPointer private14;
58     uint max_request_size;                          /* maximum number 32 bit words in request*/
59     _XrmHashBucketRec* db;
60     extern (C) nothrow int function( _XDisplay* )private15;
61     char* display_name;                             /* "host:display" string used on this connect*/
62     int default_screen;                             /* default screen for operations */
63     int nscreens;                                   /* number of screens on this server*/
64     Screen* screens;                                /* pointer to list of screens */
65     c_ulong motion_buffer;                          /* size of motion buffer */
66     c_ulong private16;
67     int min_keycode;                                /* minimum defined keycode */
68     int max_keycode;                                /* maximum defined keycode */
69     XPointer private17;
70     XPointer private18;
71     int private19;
72     char* xdefaults;                                /* contents of defaults from server */
73     /* there is more to this structure, but it is private to Xlib */
74 }
75 alias Display = _XDisplay;
76 alias _XPrivDisplay = _XDisplay*;
77 
78 struct Screen
79 {
80     XExtData *ext_data;             /* hook for extension to hang data */
81     Display* display;      /* back pointer to display structure */
82     Window root;                    /* root window ID */
83     int width, height;              /* width and height of screen */
84     int mwidth, mheight;            /* width and height of in millimeters */
85     int ndepths;                    /* number of depths possible */
86     Depth *depths;                  /* list of allowable depths on the screen */
87     int root_depth;                 /* bits per pixel */
88     Visual *root_visual;            /* root visual */
89     GC default_gc;                  /* GC for the root root visual */
90     Colormap cmap;                  /* default colormap */
91     uint white_pixel;
92     uint black_pixel;      /* white and black pixel values */
93     int max_maps, min_maps;         /* max and min colormaps */
94     int backing_store;              /* Never, WhenMapped, Always */
95     Bool save_unders;
96     long root_input_mask;           /* initial root input mask */
97 }
98 
99 struct XVisualInfo
100 {
101  	Visual* visual;
102  	VisualID visualid;
103  	int screen_num;
104  	uint depth;
105  	int class_;
106  	c_ulong  red_mask;  
107  	c_ulong  green_mask;  
108 	c_ulong  blue_mask;  
109  	int colormap_size; /* Same as map_entries member of Visual */
110  	int bits_per_rgb;
111 }
112 
113 struct Visual;
114 alias Status = int;
115 alias KeyCode = ubyte;
116 alias KeySym = XID;
117 alias Bool = int;
118 alias XPointer = char*;
119 alias Drawable = XID;
120 alias Cursor = XID;
121 alias Colormap = XID;
122 alias Pixmap = XID;
123 alias Window = XID;
124 enum None = 0;
125 enum True = 1;
126 enum False = 0;
127 enum AllocNone = 0;
128 enum AllocAll = 1;
129 
130 /*****************************************************************
131  * ERROR CODES 
132  *****************************************************************/
133 
134 enum Success		= 0;	/* everything's okay */
135 enum BadRequest	    = 1;	/* bad request code */
136 enum BadValue	    = 2;	/* int parameter out of range */
137 enum BadWindow	    = 3;	/* parameter not a Window */
138 enum BadPixmap	    = 4;	/* parameter not a Pixmap */
139 enum BadAtom		= 5;	/* parameter not an Atom */
140 enum BadCursor	    = 6;	/* parameter not a Cursor */
141 enum BadFont		= 7;	/* parameter not a Font */
142 enum BadMatch	    = 8;	/* parameter mismatch */
143 enum BadDrawable	= 9;	/* parameter not a Pixmap or Window */
144 enum BadAccess	    = 10;	/* depending on context:
145 				             - key/button already grabbed
146 				             - attempt to free an illegal 
147 				               cmap entry 
148 				            - attempt to store into a read-only 
149 				               color map entry.
150  				            - attempt to modify the access control
151 				               list from other than the local host.
152 				            */
153 enum BadAlloc	       = 11;	/* insufficient resources */
154 enum BadColor	       = 12;	/* no such colormap */
155 enum BadGC		       = 13;	/* parameter not a GC */
156 enum BadIDChoice	   = 14;	/* choice not in range or already used */
157 enum BadName		   = 15;	/* font or color name doesn't exist */
158 enum BadLength	       = 16;	/* Request length incorrect */
159 enum BadImplementation = 17;	/* server is defective */
160 
161 auto ScreenOfDisplay(Display* dpy, int scr){return &(cast(_XPrivDisplay)(dpy)).screens[scr];}
162 auto DefaultScreenOfDisplay(Display* dpy){return ScreenOfDisplay(dpy,DefaultScreen(dpy));}
163 auto DisplayOfScreen(Screen* s){return s.display;}
164 
165 auto BlackPixel(Display* dpy, int  scr)
166 {
167     return ScreenOfDisplay(dpy,scr).black_pixel;
168 }
169 auto WhitePixel(Display* dpy, int scr)
170 {
171     return ScreenOfDisplay(dpy,scr).white_pixel;
172 }
173 auto DefaultScreen(Display* dpy){return (cast(_XPrivDisplay)dpy).default_screen;}
174 auto RootWindow(Display* dpy, int scr){return ScreenOfDisplay(dpy,scr).root;}
175 auto RootWindowOfScreen(Screen* s){return s.root;}
176 
177 
178 struct XSetWindowAttributes 
179 {
180 	Pixmap background_pixmap;	/* background, None, or ParentRelative */
181 	c_ulong  background_pixel;	/* background pixel */ 
182 	Pixmap border_pixmap;		/* border of the window or CopyFromParent */
183 	c_ulong  border_pixel;	/* border pixel value */ 
184 	int bit_gravity;		/* one of bit gravity values */
185 	int win_gravity;		/* one of the window gravity values */
186 	int backing_store;		/* NotUseful, WhenMapped, Always */
187 	c_ulong  backing_planes;	/* planes to be preserved if possible */ 
188 	c_ulong  backing_pixel;	/* value to use in restoring planes */ 
189 	Bool save_under;		/* should bits under be saved? (popups) */
190 	long event_mask;		/* set of events that should be saved */
191 	long do_not_propagate_mask;	/* set of events that should not propagate */
192 	Bool override_redirect;		/* boolean value for override_redirect */
193 	Colormap colormap;		/* color map to be associated with window */
194 	Cursor cursor;			/* cursor to be displayed (or None) */
195 }
196 
197 
198 struct XWindowAttributes
199 {
200     int x, y;
201     /* location of window */
202     int width, height;
203     /* width and height of window */
204     int border_width;
205     /* border width of window */
206     int depth;
207     /* depth of window */
208     Visual *visual;
209     /* the associated visual structure */
210     Window root;
211     /* root of screen containing window */
212     int class_;
213     /* InputOutput, InputOnly*/
214     int bit_gravity;
215     /* one of the bit gravity values */
216     int win_gravity;
217     /* one of the window gravity values */
218     int backing_store;
219     /* NotUseful, WhenMapped, Always */
220     c_ulong  backing_planes;/* planes to be preserved if possible */ 
221     c_ulong  backing_pixel;/* value to be used when restoring planes */ 
222     Bool save_under;
223     /* boolean, should bits under be saved? */
224     Colormap colormap;
225     /* color map to be associated with window */
226     Bool map_installed;
227     /* boolean, is color map currently installed*/
228     int map_state;
229     /* IsUnmapped, IsUnviewable, IsViewable */
230     long all_event_masks;
231     /* set of events all people have interest in*/
232     long your_event_mask;
233     /* my event mask */
234     long do_not_propagate_mask;/* set of events that should not propagate */
235     Bool override_redirect;
236     /* boolean value for override-redirect */
237     Screen *screen;
238 
239     /* back pointer to correct screen */
240 
241 }
242 struct XWindowChanges
243 {
244     int x, y;
245     int width, height;
246     int border_width;
247     Window sibling;
248     int stack_mode;
249 }
250 
251 /*
252  * Definitions of specific events.
253  */
254 struct XKeyEvent
255 {
256 	int type;		/* of event */
257 	c_ulong  serial;	/* # of last request processed by server */ 
258 	Bool send_event;	/* true if this came from a SendEvent request */
259 	Display *display;	/* Display the event was read from */
260 	Window window;	        /* "event" window it is reported relative to */
261 	Window root;	        /* root window that the event occurred on */
262 	Window subwindow;	/* child window */
263 	Time time;		/* milliseconds */
264 	int x, y;		/* pointer x, y coordinates in event window */
265 	int x_root, y_root;	/* coordinates relative to root */
266 	uint state;	/* key or button mask */
267 	uint keycode;	/* detail */
268 	Bool same_screen;	/* same screen flag */
269 }
270 alias  XKeyPressedEvent = XKeyEvent;
271 alias  XKeyReleasedEvent = XKeyEvent;
272 enum ShiftMask   =     1; // Shift
273 enum LockMask    =     2; // Caps Lock
274 enum ControlMask =     4; // Ctrl
275 enum Mod1Mask    =     8; // Alt
276 enum Mod2Mask    =    16; // Num Lock
277 enum Mod3Mask    =    32; // Scroll Lock
278 enum Mod4Mask    =    64; // Windows
279 enum Mod5Mask    =   128; // ???
280 
281 
282 struct XButtonEvent
283 {
284 	int type;		/* of event */
285 	c_ulong  serial;	/* # of last request processed by server */ 
286 	Bool send_event;	/* true if this came from a SendEvent request */
287 	Display *display;	/* Display the event was read from */
288 	Window window;	        /* "event" window it is reported relative to */
289 	Window root;	        /* root window that the event occurred on */
290 	Window subwindow;	/* child window */
291 	Time time;		/* milliseconds */
292 	int x, y;		/* pointer x, y coordinates in event window */
293 	int x_root, y_root;	/* coordinates relative to root */
294 	uint state;	/* key or button mask */
295 	uint button;	/* detail */
296 	Bool same_screen;	/* same screen flag */
297 }
298 alias XButtonPressedEvent = XButtonEvent;
299 alias XButtonReleasedEvent = XButtonEvent;
300 
301 struct XMotionEvent 
302 {
303 	int type;		/* of event */
304 	c_ulong  serial;	/* # of last request processed by server */ 
305 	Bool send_event;	/* true if this came from a SendEvent request */
306 	Display *display;	/* Display the event was read from */
307 	Window window;	        /* "event" window reported relative to */
308 	Window root;	        /* root window that the event occurred on */
309 	Window subwindow;	/* child window */
310 	Time time;		/* milliseconds */
311 	int x, y;		/* pointer x, y coordinates in event window */
312 	int x_root, y_root;	/* coordinates relative to root */
313 	uint state;	/* key or button mask */
314 	char is_hint;		/* detail */
315 	Bool same_screen;	/* same screen flag */
316 }
317 alias XPointerMovedEvent = XMotionEvent;
318 
319 struct XCrossingEvent {
320 	int type;		/* of event */
321 	c_ulong  serial;	/* # of last request processed by server */ 
322 	Bool send_event;	/* true if this came from a SendEvent request */
323 	Display *display;	/* Display the event was read from */
324 	Window window;	        /* "event" window reported relative to */
325 	Window root;	        /* root window that the event occurred on */
326 	Window subwindow;	/* child window */
327 	Time time;		/* milliseconds */
328 	int x, y;		/* pointer x, y coordinates in event window */
329 	int x_root, y_root;	/* coordinates relative to root */
330 	int mode;		/* NotifyNormal, NotifyGrab, NotifyUngrab */
331 	int detail;
332 	/*
333 	 * NotifyAncestor, NotifyVirtual, NotifyInferior,
334 	 * NotifyNonlinear,NotifyNonlinearVirtual
335 	 */
336 	Bool same_screen;	/* same screen flag */
337 	Bool focus;		/* boolean focus */
338 	uint state;	/* key or button mask */
339 }
340 alias XEnterWindowEvent =  XCrossingEvent;
341 alias XLeaveWindowEvent =  XCrossingEvent;
342 
343 struct XFocusChangeEvent {
344 	int type;		/* FocusIn or FocusOut */
345 	c_ulong  serial;	/* # of last request processed by server */ 
346 	Bool send_event;	/* true if this came from a SendEvent request */
347 	Display *display;	/* Display the event was read from */
348 	Window window;		/* window of event */
349 	int mode;		/* NotifyNormal, NotifyWhileGrabbed,
350 				   NotifyGrab, NotifyUngrab */
351 	int detail;
352 	/*
353 	 * NotifyAncestor, NotifyVirtual, NotifyInferior,
354 	 * NotifyNonlinear,NotifyNonlinearVirtual, NotifyPointer,
355 	 * NotifyPointerRoot, NotifyDetailNone
356 	 */
357 }
358 alias XFocusInEvent = XFocusChangeEvent;
359 alias XFocusOutEvent = XFocusChangeEvent;
360 
361 /* generated on EnterWindow and FocusIn  when KeyMapState selected */
362 struct XKeymapEvent
363 {
364 	int type;
365 	c_ulong  serial;	/* # of last request processed by server */ 
366 	Bool send_event;	/* true if this came from a SendEvent request */
367 	Display *display;	/* Display the event was read from */
368 	Window window;
369 	char[32] key_vector;
370 }
371 
372 struct XExposeEvent
373 {
374 	int type;
375 	c_ulong  serial;	/* # of last request processed by server */ 
376 	Bool send_event;	/* true if this came from a SendEvent request */
377 	Display *display;	/* Display the event was read from */
378 	Window window;
379 	int x, y;
380 	int width, height;
381 	int count;		/* if non-zero, at least this many more */
382 }
383 
384 struct XGraphicsExposeEvent
385 {
386 	int type;
387 	c_ulong  serial;	/* # of last request processed by server */ 
388 	Bool send_event;	/* true if this came from a SendEvent request */
389 	Display *display;	/* Display the event was read from */
390 	Drawable drawable;
391 	int x, y;
392 	int width, height;
393 	int count;		/* if non-zero, at least this many more */
394 	int major_code;		/* core is CopyArea or CopyPlane */
395 	int minor_code;		/* not defined in the core */
396 }
397 
398 struct XNoExposeEvent
399 {
400 	int type;
401 	c_ulong  serial;	/* # of last request processed by server */ 
402 	Bool send_event;	/* true if this came from a SendEvent request */
403 	Display *display;	/* Display the event was read from */
404 	Drawable drawable;
405 	int major_code;		/* core is CopyArea or CopyPlane */
406 	int minor_code;		/* not defined in the core */
407 }
408 
409 struct XVisibilityEvent
410 {
411 	int type;
412 	c_ulong  serial;	/* # of last request processed by server */ 
413 	Bool send_event;	/* true if this came from a SendEvent request */
414 	Display *display;	/* Display the event was read from */
415 	Window window;
416 	int state;		/* Visibility state */
417 }
418 
419 struct XCreateWindowEvent
420 {
421 	int type;
422 	c_ulong  serial;	/* # of last request processed by server */ 
423 	Bool send_event;	/* true if this came from a SendEvent request */
424 	Display *display;	/* Display the event was read from */
425 	Window parent;		/* parent of the window */
426 	Window window;		/* window id of window created */
427 	int x, y;		/* window location */
428 	int width, height;	/* size of window */
429 	int border_width;	/* border width */
430 	Bool override_redirect;	/* creation should be overridden */
431 }
432 
433 struct XDestroyWindowEvent
434 {
435 	int type;
436 	c_ulong  serial;	/* # of last request processed by server */ 
437 	Bool send_event;	/* true if this came from a SendEvent request */
438 	Display *display;	/* Display the event was read from */
439 	Window event;
440 	Window window;
441 }
442 
443 struct XUnmapEvent
444 {
445 	int type;
446 	c_ulong  serial;	/* # of last request processed by server */ 
447 	Bool send_event;	/* true if this came from a SendEvent request */
448 	Display *display;	/* Display the event was read from */
449 	Window event;
450 	Window window;
451 	Bool from_configure;
452 }
453 
454 struct XMapEvent
455 {
456 	int type;
457 	c_ulong  serial;	/* # of last request processed by server */ 
458 	Bool send_event;	/* true if this came from a SendEvent request */
459 	Display *display;	/* Display the event was read from */
460 	Window event;
461 	Window window;
462 	Bool override_redirect;	/* boolean, is override set... */
463 }
464 
465 struct XMapRequestEvent
466 {
467 	int type;
468 	c_ulong  serial;	/* # of last request processed by server */ 
469 	Bool send_event;	/* true if this came from a SendEvent request */
470 	Display *display;	/* Display the event was read from */
471 	Window parent;
472 	Window window;
473 }
474 
475 struct XReparentEvent
476 {
477 	int type;
478 	c_ulong  serial;	/* # of last request processed by server */ 
479 	Bool send_event;	/* true if this came from a SendEvent request */
480 	Display *display;	/* Display the event was read from */
481 	Window event;
482 	Window window;
483 	Window parent;
484 	int x, y;
485 	Bool override_redirect;
486 }
487 
488 struct XConfigureEvent
489 {
490 	int type;
491 	c_ulong  serial;	/* # of last request processed by server */ 
492 	Bool send_event;	/* true if this came from a SendEvent request */
493 	Display *display;	/* Display the event was read from */
494 	Window event;
495 	Window window;
496 	int x, y;
497 	int width, height;
498 	int border_width;
499 	Window above;
500 	Bool override_redirect;
501 }
502 
503 struct XGravityEvent
504 {
505 	int type;
506 	c_ulong  serial;	/* # of last request processed by server */ 
507 	Bool send_event;	/* true if this came from a SendEvent request */
508 	Display *display;	/* Display the event was read from */
509 	Window event;
510 	Window window;
511 	int x, y;
512 }
513 
514 struct XResizeRequestEvent
515 {
516 	int type;
517 	c_ulong  serial;	/* # of last request processed by server */ 
518 	Bool send_event;	/* true if this came from a SendEvent request */
519 	Display *display;	/* Display the event was read from */
520 	Window window;
521 	int width, height;
522 }
523 
524 struct XConfigureRequestEvent
525 {
526 	int type;
527 	c_ulong  serial;	/* # of last request processed by server */ 
528 	Bool send_event;	/* true if this came from a SendEvent request */
529 	Display *display;	/* Display the event was read from */
530 	Window parent;
531 	Window window;
532 	int x, y;
533 	int width, height;
534 	int border_width;
535 	Window above;
536 	int detail;		/* Above, Below, TopIf, BottomIf, Opposite */
537 	c_ulong  value_mask; 
538 }
539 
540 struct XCirculateEvent
541 {
542 	int type;
543 	c_ulong  serial;	/* # of last request processed by server */ 
544 	Bool send_event;	/* true if this came from a SendEvent request */
545 	Display *display;	/* Display the event was read from */
546 	Window event;
547 	Window window;
548 	int place;		/* PlaceOnTop, PlaceOnBottom */
549 }
550 
551 struct XCirculateRequestEvent
552 {
553 	int type;
554 	c_ulong  serial;	/* # of last request processed by server */ 
555 	Bool send_event;	/* true if this came from a SendEvent request */
556 	Display *display;	/* Display the event was read from */
557 	Window parent;
558 	Window window;
559 	int place;		/* PlaceOnTop, PlaceOnBottom */
560 }
561 
562 struct XPropertyEvent
563 {
564 	int type;
565 	c_ulong  serial;	/* # of last request processed by server */ 
566 	Bool send_event;	/* true if this came from a SendEvent request */
567 	Display *display;	/* Display the event was read from */
568 	Window window;
569 	Atom atom;
570 	Time time;
571 	int state;		/* NewValue, Deleted */
572 }
573 
574 struct XSelectionClearEvent
575 {
576 	int type;
577 	c_ulong  serial;	/* # of last request processed by server */ 
578 	Bool send_event;	/* true if this came from a SendEvent request */
579 	Display *display;	/* Display the event was read from */
580 	Window window;
581 	Atom selection;
582 	Time time;
583 }
584 
585 struct XSelectionRequestEvent
586 {
587 	int type;
588 	c_ulong  serial;	/* # of last request processed by server */ 
589 	Bool send_event;	/* true if this came from a SendEvent request */
590 	Display *display;	/* Display the event was read from */
591 	Window owner;
592 	Window requestor;
593 	Atom selection;
594 	Atom target;
595 	Atom property;
596 	Time time;
597 }
598 
599 struct XSelectionEvent
600 {
601 	int type;
602 	c_ulong  serial;	/* # of last request processed by server */ 
603 	Bool send_event;	/* true if this came from a SendEvent request */
604 	Display *display;	/* Display the event was read from */
605 	Window requestor;
606 	Atom selection;
607 	Atom target;
608 	Atom property;		/* ATOM or None */
609 	Time time;
610 }
611 
612 struct XColormapEvent
613 {
614 	int type;
615 	c_ulong  serial;	/* # of last request processed by server */ 
616 	Bool send_event;	/* true if this came from a SendEvent request */
617 	Display *display;	/* Display the event was read from */
618 	Window window;
619 	Colormap colormap;	/* COLORMAP or None */
620 	Bool c_new;
621 	int state;		/* ColormapInstalled, ColormapUninstalled */
622 }
623 
624 struct XClientMessageEvent
625 {
626 	int type;
627 	c_ulong  serial;	/* # of last request processed by server */ 
628 	Bool send_event;	/* true if this came from a SendEvent request */
629 	Display *display;	/* Display the event was read from */
630 	Window window;
631 	Atom message_type;
632 	int format;
633 	union DataUnion {
634 		char[20] b;
635 		short[10] s;
636 		long[5] l;
637     }
638     DataUnion data;
639 
640 }
641 
642 struct XMappingEvent
643 {
644 	int type;
645 	c_ulong  serial;	/* # of last request processed by server */ 
646 	Bool send_event;	/* true if this came from a SendEvent request */
647 	Display *display;	/* Display the event was read from */
648 	Window window;		/* unused */
649 	int request;		/* one of MappingModifier, MappingKeyboard,
650 				   MappingPointer */
651 	int first_keycode;	/* first keycode */
652 	int count;		/* defines range of change w. first_keycode*/
653 }
654 
655 struct XErrorEvent
656 {
657 	int type;
658 	Display *display;	/* Display the event was read from */
659 	XID resourceid;		/* resource id */
660 	c_ulong  serial;	/* serial number of failed request */ 
661 	ubyte error_code;	/* error code of failed request */
662 	ubyte request_code;	/* Major op-code of failed request */
663 	ubyte minor_code;	/* Minor op-code of failed request */
664 }
665 
666 struct XAnyEvent
667 {
668 	int type;
669 	c_ulong  serial;	/* # of last request processed by server */ 
670 	Bool send_event;	/* true if this came from a SendEvent request */
671 	Display *display;/* Display the event was read from */
672 	Window window;	/* window on which event was requested in event mask */
673 }
674 
675 
676 /***************************************************************
677  *
678  * GenericEvent.  This event is the standard event for all newer extensions.
679  */
680 
681 struct XGenericEvent
682 {
683     int            type;         /* of event. Always GenericEvent */
684     c_ulong   serial;       /* # of last request processed */ 
685     Bool           send_event;   /* true if from SendEvent request */
686     Display        *display;     /* Display the event was read from */
687     int            extension;    /* major opcode of extension that caused the event */
688     int            evtype;       /* actual event type. */
689 }
690 
691 struct XGenericEventCookie
692 {
693     int            type;         /* of event. Always GenericEvent */
694     c_ulong   serial;       /* # of last request processed */ 
695     Bool           send_event;   /* true if from SendEvent request */
696     Display        *display;     /* Display the event was read from */
697     int            extension;    /* major opcode of extension that caused the event */
698     int            evtype;       /* actual event type. */
699     uint   cookie;
700     void           *data;
701 }
702 
703 struct XComposeStatus
704 {
705     XPointer compose_ptr;
706     int chars_matched;
707 }
708 
709 enum {
710     NoEventMask          = 0,
711     KeyPressMask         = 1L << 0,
712     KeyReleaseMask       = 1L << 1,
713     KeymapStateMask      = 1L << 14,
714     ExposureMask         = 1L << 15,
715     VisibilityChangeMask = 1L << 16,
716     ResizeRedirectMask   = 1L << 18,
717     ButtonPressMask      = 1L << 2,
718     ButtonReleaseMask    = 1L << 3,
719     EnterWindowMask      = 1L << 4,
720     LeaveWindowMask      = 1L << 5,
721     PointerMotionMask    = 1L << 6,
722     PointerMotionHintMask= 1L << 7,
723     Button1MotionMask    = 1L << 8,
724     Button2MotionMask    = 1L << 9,
725 }
726 
727 enum KeyPress		= 2;
728 enum KeyRelease		= 3;
729 enum ButtonPress		= 4;
730 enum ButtonRelease		= 5;
731 enum MotionNotify		= 6;
732 enum EnterNotify		= 7;
733 enum LeaveNotify		= 8;
734 enum FocusIn			= 9;
735 enum FocusOut		= 10;
736 enum KeymapNotify		= 11;
737 enum Expose			= 12;
738 enum GraphicsExpose		= 13;
739 enum NoExpose		= 14;
740 enum VisibilityNotify	= 15;
741 enum CreateNotify		= 16;
742 enum DestroyNotify		= 17;
743 enum UnmapNotify		= 18;
744 enum MapNotify		= 19;
745 enum MapRequest		= 20;
746 enum ReparentNotify		= 21;
747 enum ConfigureNotify		= 22;
748 enum ConfigureRequest	= 23;
749 enum GravityNotify		= 24;
750 enum ResizeRequest		= 25;
751 enum CirculateNotify		= 26;
752 enum CirculateRequest	= 27;
753 enum PropertyNotify		= 28;
754 enum SelectionClear		= 29;
755 enum SelectionRequest	= 30;
756 enum SelectionNotify		= 31;
757 enum ColormapNotify		= 32;
758 enum ClientMessage		= 33;
759 enum MappingNotify		= 34;
760 enum GenericEvent		= 35;
761 enum LASTEvent		= 36	/* must be bigger than any event # */;
762 
763 enum InputOutput = 1;
764 enum InputOnly   = 2;
765 enum CWX		   = (1<<0);
766 enum CWY	       = (1<<1);
767 enum CWWidth	   = (1<<2);
768 enum CWHeight	   = (1<<3);
769 enum CWBorderWidth = (1<<4);
770 enum CWSibling	   = (1<<5);
771 enum CWStackMode   = (1<<6);
772 
773 enum CWBackPixmap		= (1L<<0);
774 enum CWBackPixel		= (1L<<1);
775 enum CWBorderPixmap		= (1L<<2);
776 enum CWBorderPixel		= (1L<<3);
777 enum CWBitGravity		= (1L<<4);
778 enum CWWinGravity		= (1L<<5);
779 enum CWBackingStore		= (1L<<6);
780 enum CWBackingPlanes	= (1L<<7);
781 enum CWBackingPixel		= (1L<<8);
782 enum CWOverrideRedirect	= (1L<<9);
783 enum CWSaveUnder		= (1L<<10);
784 enum CWEventMask		= (1L<<11);
785 enum CWDontPropagate	= (1L<<12);
786 enum CWColormap		    = (1L<<13);
787 enum CWCursor		    = (1L<<14);
788 
789 union XEvent 
790 {
791     int type;		/* must not be changed; first element */
792 	XAnyEvent xany;
793 	XKeyEvent xkey;
794 	XButtonEvent xbutton;
795 	XMotionEvent xmotion;
796 	XCrossingEvent xcrossing;
797 	XFocusChangeEvent xfocus;
798 	XExposeEvent xexpose;
799 	XGraphicsExposeEvent xgraphicsexpose;
800 	XNoExposeEvent xnoexpose;
801 	XVisibilityEvent xvisibility;
802 	XCreateWindowEvent xcreatewindow;
803 	XDestroyWindowEvent xdestroywindow;
804 	XUnmapEvent xunmap;
805 	XMapEvent xmap;
806 	XMapRequestEvent xmaprequest;
807 	XReparentEvent xreparent;
808 	XConfigureEvent xconfigure;
809 	XGravityEvent xgravity;
810 	XResizeRequestEvent xresizerequest;
811 	XConfigureRequestEvent xconfigurerequest;
812 	XCirculateEvent xcirculate;
813 	XCirculateRequestEvent xcirculaterequest;
814 	XPropertyEvent xproperty;
815 	XSelectionClearEvent xselectionclear;
816 	XSelectionRequestEvent xselectionrequest;
817 	XSelectionEvent xselection;
818 	XColormapEvent xcolormap;
819 	XClientMessageEvent xclient;
820 	XMappingEvent xmapping;
821 	XErrorEvent xerror;
822 	XKeymapEvent xkeymap;
823 	XGenericEvent xgeneric;
824 	XGenericEventCookie xcookie;
825 	long[24] pad;
826 }
827 
828 
829 
830 
831 version(SharedX11)
832 {
833 	extern(C) nothrow @nogc __gshared
834 	{
835 		Display*  function(const(char)* display_name) XOpenDisplay;
836 		int function(Display* display) XCloseDisplay;
837 		
838 		Window function (Display *display,
839 		Window parent,
840 		int x, int y,
841 		uint width, uint height,
842 		uint border_width,
843 		ulong border,
844 		ulong background) XCreateSimpleWindow;
845 
846 		Window function(
847 		Display* display,
848 		Window parent,
849 		int x, int y,
850 		uint width, uint height,
851 		uint border_width,
852 		int depth,
853 		uint _class,
854 		Visual* visual,
855 		c_ulong  valuemask, 
856 		XSetWindowAttributes* attributes
857 		) XCreateWindow;
858 		int function(Display *display, Window w) XClearWindow;
859 		int function(Display *display, Window w) XMapRaised;
860 		int function(Display *display, XEvent *event_return) XNextEvent;
861 		int function(Display *display, XEvent *event_return) XPeekEvent;
862 		int function(Display *display, Window w) XDestroyWindow;
863 		int function(Display *display, Window w, long event_mask) XSelectInput;
864 		Status function(Display* display, Window w, XWindowAttributes* win_attr) XGetWindowAttributes;
865 		int function(XMappingEvent *event_map) XRefreshKeyboardMapping;
866 		int function(XKeyEvent *event_struct, char *buffer_return, int bytes_buffer, KeySym *keysym_return, XComposeStatus *status_in_out) XLookupString;
867 		KeySym function(Display* display, typeof(XKeyEvent.keycode) keycode, int index) XKeycodeToKeysym;
868 		Colormap function(Display *display, Window w, Visual *visual, int alloc) XCreateColormap;
869 		int function(Display *display, Window w, const(char)* window_name) XStoreName;
870 		int function(Display* display,Window w,uint value_mask,XWindowChanges* values) XConfigureWindow;
871 		int function(void* data) XFree;
872 		int function(Display* display,Colormap colormap) XFreeColormap;
873 		int function (Display *display) XFlush;
874 		int function (Display *display, Bool discard) XSync;
875 		int function(XErrorHandler handler) XSetErrorHandler;
876 		Atom function (Display *display, const(char)* atom_name, Bool only_if_exists) XInternAtom;
877 		Status function(Display* display, Window w, Atom* protocols, int count) XSetWMProtocols;
878 		int function(Display* display) XPending;
879 	
880 
881 
882 		//GL
883 		XVisualInfo* function(Display* display,int screen,int* attribList) glXChooseVisual;
884 		GLXContext function(Display* display, XVisualInfo* vis, GLXContext shareList, Bool direct) glXCreateContext;
885 		Bool function(Display* dpy,GLXDrawable drawable, GLXContext ctx) glXMakeCurrent;
886 		void function(Display* dpy, GLXDrawable drawable) glXSwapBuffers;
887 		void function(Display* dpy, GLXContext ctx) glXDestroyContext;
888 		Bool function (Display * dpy, int * major,int * minor) glXQueryVersion;
889 		GLXFBConfig* function(Display * dpy,int screen,const(int)* attrib_list,int* nelements) glXChooseFBConfig;
890 		const (char)* function(	Display * dpy,int screen) glXQueryExtensionsString;
891 		void* function (const GLubyte* procName) glXGetProcAddressARB;
892 		GLXContext function(Display * dpy, GLXFBConfig config,int render_type,GLXContext share_list,Bool direct)  glXCreateNewContext;
893 		XVisualInfo* function(Display * dpy, GLXFBConfig config) glXGetVisualFromFBConfig;
894 		int function(Display* dpy, GLXFBConfig config,int attribute,int* value) glXGetFBConfigAttrib;
895 		Bool function(Display* dpy, GLXContext context) glXIsDirect;
896 
897 
898 	}
899 }
900 else
901 {
902 	extern(C) nothrow @nogc __gshared extern
903 	{
904 		Display* XOpenDisplay(const(char)* display_name);
905 		int  XCloseDisplay(Display* display);
906 		
907 		Window  XCreateSimpleWindow(Display* display,
908 		Window parent,
909 		int x, int y,
910 		uint width, uint height,
911 		uint border_width,
912 		ulong border,
913 		ulong background);
914 
915 		Window XCreateWindow (
916 		Display* display,
917 		Window parent,
918 		int x, int y,
919 		uint width, uint height,
920 		uint border_width,
921 		int depth,
922 		uint _class,
923 		Visual* visual,
924 		c_ulong  valuemask, 
925 		XSetWindowAttributes* attributes
926 		);
927 		int  XClearWindow(Display *display, Window w);
928 		int  XMapRaised(Display *display, Window w);
929 		int  XNextEvent(Display *display, XEvent *event_return);
930 		int  XPeekEvent(Display *display, XEvent *event_return);
931 		int  XDestroyWindow(Display *display, Window w);
932 		int  XSelectInput(Display *display, Window w, long event_mask);
933 		Status  XGetWindowAttributes(Display* display, Window w, XWindowAttributes* win_attr);
934 		int  XRefreshKeyboardMapping(XMappingEvent *event_map);
935 		int  XLookupString(XKeyEvent *event_struct, char *buffer_return, int bytes_buffer, KeySym *keysym_return, XComposeStatus *status_in_out);
936 		KeySym XKeycodeToKeysym(Display* display, typeof(XKeyEvent.keycode) keycode, int index);
937 		
938 		Colormap  XCreateColormap(Display *display, Window w, Visual *visual, int alloc);
939 		int  XStoreName(Display *display, Window w, const(char)* window_name);
940 		int  XConfigureWindow(Display* display,Window w,uint value_mask,XWindowChanges* values);
941 		int  XFree(void* data);
942 		int  XFreeColormap(Display* display,Colormap colormap);
943 		int  XFlush(Display *display);
944 		int  XSync(Display *display, Bool discard);
945 		int  XSetErrorHandler(XErrorHandler handler);
946 		Atom XInternAtom(Display *display, const(char)* atom_name, Bool only_if_exists);
947 		Status XSetWMProtocols(Display* display, Window w, Atom* protocols, int count);
948 		int XPending(Display* display);
949 
950 
951 		//GL
952 		XVisualInfo* glXChooseVisual(Display* display,int screen,int* attribList);
953 		GLXContext  glXCreateContext(Display* display, XVisualInfo* vis, GLXContext shareList, Bool direct);
954 		Bool  glXMakeCurrent(Display* dpy,GLXDrawable drawable, GLXContext ctx);
955 		void  glXSwapBuffers(Display* dpy, GLXDrawable drawable);
956 		void  glXDestroyContext(Display* dpy, GLXContext ctx);
957 		Bool glXQueryVersion(Display * dpy, int * major,int * minor);
958 		GLXFBConfig* glXChooseFBConfig(Display * dpy,int screen,const(int)* attrib_list,int* nelements);
959 		const (char)* glXQueryExtensionsString(	Display * dpy,int screen);
960 		void *glXGetProcAddressARB(const(GLubyte)* procName);
961 		GLXContext glXCreateNewContext(	Display * dpy, GLXFBConfig config,int render_type,GLXContext share_list,Bool direct);
962 		XVisualInfo* glXGetVisualFromFBConfig(Display * dpy, GLXFBConfig config);
963 		int glXGetFBConfigAttrib(Display* dpy, GLXFBConfig config,int attribute,int* value);
964 		Bool glXIsDirect(Display* dpy, GLXContext context);
965 
966 
967 	}
968 }
969 version(SharedX11):
970 
971 package void* dl;
972 
973 void loadX11()
974 {
975     static bool hasStart = false;
976     if(!hasStart)
977     {
978         import core.sys.linux.dlfcn;
979         import core.stdc.stdio:printf;
980 		//There is some cases which seems X11 is able to load even when the 'dl' is null.
981         dl = dlopen("X11", RTLD_LAZY); 
982 		// if(dl == null)
983 		// {
984 		// 	printf("Could not find libX11.so\n");
985 		// 	return;
986 		// }
987         void* load_dl_func(const(char*) func)
988         {
989             void* ret =  dlsym(dl, func);
990             if(ret == null)
991                 printf("Could not load %s:\n", func);
992             return ret;
993         }
994 
995         XOpenDisplay = cast(typeof(XOpenDisplay))load_dl_func("XOpenDisplay");
996         XCloseDisplay = cast(typeof(XCloseDisplay))load_dl_func("XCloseDisplay");
997         XCreateSimpleWindow = cast(typeof(XCreateSimpleWindow))load_dl_func("XCreateSimpleWindow");
998         XCreateWindow = cast(typeof(XCreateWindow))load_dl_func("XCreateWindow");
999         XClearWindow = cast(typeof(XClearWindow))load_dl_func("XClearWindow");
1000         XMapRaised = cast(typeof(XMapRaised))load_dl_func("XMapRaised");
1001         XNextEvent = cast(typeof(XNextEvent))load_dl_func("XNextEvent");
1002         XPeekEvent = cast(typeof(XPeekEvent))load_dl_func("XPeekEvent");
1003         XDestroyWindow = cast(typeof(XDestroyWindow))load_dl_func("XDestroyWindow");
1004         XSelectInput = cast(typeof(XSelectInput))load_dl_func("XSelectInput");
1005         XGetWindowAttributes = cast(typeof(XGetWindowAttributes))load_dl_func("XGetWindowAttributes");
1006         XRefreshKeyboardMapping = cast(typeof(XRefreshKeyboardMapping))load_dl_func("XRefreshKeyboardMapping");
1007         XLookupString = cast(typeof(XLookupString))load_dl_func("XLookupString");
1008 		XKeycodeToKeysym = cast(typeof(XKeycodeToKeysym))load_dl_func("XKeycodeToKeysym");
1009         XCreateColormap = cast(typeof(XCreateColormap))load_dl_func("XCreateColormap");
1010         XStoreName = cast(typeof(XStoreName))load_dl_func("XStoreName");
1011         XConfigureWindow = cast(typeof(XConfigureWindow))load_dl_func("XConfigureWindow");
1012         XFree = cast(typeof(XFree))load_dl_func("XFree");
1013         XFreeColormap = cast(typeof(XFreeColormap))load_dl_func("XFreeColormap");
1014         XFlush = cast(typeof(XFlush))load_dl_func("XFlush");
1015         XSync = cast(typeof(XSync))load_dl_func("XSync");
1016         XSetErrorHandler = cast(typeof(XSetErrorHandler))load_dl_func("XSetErrorHandler");
1017         XInternAtom = cast(typeof(XInternAtom))load_dl_func("XInternAtom");
1018         XSetWMProtocols = cast(typeof(XSetWMProtocols))load_dl_func("XSetWMProtocols");
1019         XPending = cast(typeof(XPending))load_dl_func("XPending");
1020 		
1021 
1022         glXChooseVisual = cast(typeof(glXChooseVisual))load_dl_func("glXChooseVisual");
1023         glXCreateContext = cast(typeof(glXCreateContext))load_dl_func("glXCreateContext");
1024         glXMakeCurrent = cast(typeof(glXMakeCurrent))load_dl_func("glXMakeCurrent");
1025         glXSwapBuffers = cast(typeof(glXSwapBuffers))load_dl_func("glXSwapBuffers");
1026         glXDestroyContext = cast(typeof(glXDestroyContext))load_dl_func("glXDestroyContext");
1027 		glXQueryVersion = cast(typeof(glXQueryVersion))load_dl_func("glXQueryVersion");
1028 		glXChooseFBConfig = cast(typeof(glXChooseFBConfig))load_dl_func("glXChooseFBConfig");
1029 		glXQueryExtensionsString = cast(typeof(glXQueryExtensionsString))load_dl_func("glXQueryExtensionsString");
1030 		glXGetProcAddressARB = cast(typeof(glXGetProcAddressARB))load_dl_func("glXGetProcAddressARB");
1031 		glXCreateNewContext = cast(typeof(glXCreateNewContext))load_dl_func("glXCreateNewContext");
1032 		glXGetVisualFromFBConfig = cast(typeof(glXGetVisualFromFBConfig))load_dl_func("glXGetVisualFromFBConfig");
1033 		glXGetFBConfigAttrib = cast(typeof(glXGetFBConfigAttrib))load_dl_func("glXGetFBConfigAttrib");
1034 		glXIsDirect = cast(typeof(glXIsDirect))load_dl_func("glXIsDirect");
1035 
1036 
1037         hasStart = true;
1038     }
1039 }
1040 
1041 void unloadX11()
1042 {
1043 	import core.stdc.stdio;
1044 	import core.sys.linux.dlfcn;
1045 	if(dl != null && !dlclose(dl))
1046 		printf("Could not unload X11.\n");
1047 }